package panda.client.ui.widgets;

import panda.client.JSNI;
import panda.client.Cardpanda;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;

/**
 * The noteTip a status message. It gets passed into models so other clients can get this client's latest status
 * message. Status messages meant for other clients are similar sentences but worded differently with the appropriate
 * pronouns and possessives.<br>
 * <br>
 * If a client wishes to only for itself to see its update message, it calls logPersonalNote(message).<br>
 * A client wishing to let other clients know what it just did calls log(message, messageForOtherClients).
 * 
 * @author zane.tian@gmail.com
 */
public class NoteTip extends HTML {
	private static NoteTip note = new NoteTip("");
	public static final String GradientURL = "counters/final-half.png";

	// private String latestUpdateFromThisClient; /* "You tap Forest[231]" */
	private String latestUpdateForOtherClients; /* "[Kyruru] taps Forest[231] */

	private NoteTip(String html) {
		super(html);
		setWordWrap(false);
		setShadedStyle(getElement());
		DOM.setStyleAttribute(getElement(), "border", "1px solid rgb(0, 28, 92)");
	}

	/**
	 * Sets color, padding, backgroundColor, backgroundimage, and backgroundposition
	 * 
	 * @param el
	 */
	public static void setShadedStyle(Element el) {
		DOM.setStyleAttribute(el, "color", "white");
		DOM.setStyleAttribute(el, "padding", "0px 4px 0px 4px");

		if(JSNI.getBrowserString().contains("msie 6.0")) {
			/* don't enable the transparent png under ie 6.0 */
			DOM.setStyleAttribute(el, "backgroundColor", "rgb(0, 90, 180)");
		} else {
			DOM.setStyleAttribute(el, "backgroundColor", "rgb(65, 160, 255)"); // rgb(65, 160, 255)
			DOM.setStyleAttribute(el, "backgroundImage", "url(" + NoteTip.GradientURL + ")");
			DOM.setStyleAttribute(el, "backgroundPosition", "bottom");
		}

		JSNI.disableSelection(el);
	}

	private void setMyColor() {
		if(JSNI.getBrowserString().contains("msie 6.0")) {
			DOM.setStyleAttribute(getElement(), "backgroundColor", "rgb(0, 90, 180)");
		} else {
			DOM.setStyleAttribute(getElement(), "backgroundColor", "rgb(65, 160, 255)");
		}
	}

	private void setOpponentColor() {
		if(JSNI.getBrowserString().contains("msie 6.0")) {
			DOM.setStyleAttribute(getElement(), "backgroundColor", "rgb(150, 0, 20)");
		} else {
			DOM.setStyleAttribute(getElement(), "backgroundColor", "rgb(220, 0, 70)");
		}
	}

	public static NoteTip get() {
		return note;
	}

	/**
	 * Sets the NoteTip to 'whatThisClientSees' and stores the value of 'whatOtherClientsSee'. When the model is sent to
	 * the server as an update, the server sends 'whatOtherClientsSee' to all waiting() clients.
	 */
	public void log(String whatThisClientSees, String whatOtherClientsSee) {
		logPersonalNote(whatThisClientSees, true);

		latestUpdateForOtherClients = whatOtherClientsSee;
	}

	/**
	 * Logs something that only the client needs to see. The message is color coded based on client.
	 */
	public void logPersonalNote(String result, boolean mine) {
		if(mine) {
			setMyColor();
		} else {
			setOpponentColor();
		}

		String thisClientsName = Cardpanda.clientModel.getSessionID().getName();
		setHTML(result.replace(thisClientsName + "'s", "your"));
		Window.setTitle("[" + Cardpanda.clientModel.getSessionID().getName() + "] "
				+ result.replace(thisClientsName + "'s", "your"));
	}

	public String getLatestUpdateForOtherClients() {
		return latestUpdateForOtherClients;
	}

	public void multiLineLog(String html) {
		setHTML(html + "<br>" + getHTML());
	}

	public void clearLog() {
		setHTML("");
	}
}